<template>
  <div class="content">
    <!--图片-->
    <el-row>
      <el-col :span="16">
        <img src="../../assets/image/shoubiao.png" alt="" style="width: 100%" />
      </el-col>
      <!--卡片-->
      <el-col :span="8">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>会员登录 USER LOGIN</span>
            </div>
          </template>
          <!-- 登录表单区域 -->
          <el-form label-width="0px" class="login_form" :model="user">
            <!-- 用户名 -->
            <el-form-item>
              <el-input
                prefix-icon="el-icon-user"
                v-model="user.mobile"
              ></el-input>
            </el-form-item>
            <!-- 密码 -->
            <el-form-item>
              <el-input
                prefix-icon="el-icon-lock"
                type="password"
                show-password
                v-model="user.encodedPassword"
              ></el-input>
            </el-form-item>
            <!--验证码-->
            <el-form-item>
              <el-input
                prefix-icon="el-icon-picture-outline"
                v-model="user.captcha"
              ></el-input>
              <sidentify :identifyCode="ver"></sidentify>
              <el-link :underline="false" @click.prevent="refresh">刷新验证码</el-link>
              <!-- 按钮区域 -->
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                style="width: 100%; text-align: center"
                @click="onlogin"
                >登录</el-button>
              <!--注册 密码-->
              <div class="card-footer">
                <router-link to="/register"
                  ><el-link :underline="false" type="primary"
                    >立即注册</el-link
                  ></router-link
                >
                <el-link :underline="false" @click.prevent="toforgot">忘记密码</el-link>
              </div>
            </el-form-item>
          </el-form>

          <el-divider></el-divider>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { reactive, ref } from "vue";
import { login } from '@/api/user.js'
import { verification } from "@/api/verification";
import { ElMessage } from 'element-plus'
import router from '@/router/index.js';
import Sidentify from "../../components/Sidentify.vue";
import { useStore } from 'vuex';
export default {
  name: "Login",
  setup() {
    // 表单信息
    let user = reactive({
      mobile: "",
      encodedPassword: "",
      captcha: "",
    });
    // 验证码
    let ver = ref("");
    function veri() {
      verification().then((res) => {
        ver.value = res.data.data.captcha;
      });
    }
    veri();
    function refresh() {
      veri();
    }

    // 登录
    
    let store =useStore()
    function onlogin(){
      login(user).then(res =>{
        if(res.data.code == 401){
          ElMessage.error(res.data.msg)
          return
        }
        store.commit('setid',res.data.data.id)
        localStorage.setItem("token", res.data.data.token)
        localStorage.setItem("isAuth", res.data.data.isAuth)
        localStorage.setItem('id', res.data.data.id)
        ElMessage.success("登录成功")
        router.push({path: '/'})
      })
    }
    function toforgot(){
      router.push({path:'/forget'})
    }
    return {
      user,
      ver,
      refresh,
      onlogin,
      toforgot
    };
  },
  components: { Sidentify }
};
</script>

<style scoped>
.content {
  background-color: mistyrose;
  width: 100%;
}
.el-row {
  width: 80%;
  margin: 0 auto;
}
.card-header {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 16px;
}
.el-card {
  margin: 10% 0;
  width: 100%;
  font-size: 4px;
}
.el-card .card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.el-link {
  font-size: 12px;
}
</style>
